package customer

import (
	"counter-help/admin/server/global"
	"counter-help/admin/server/model/common/response"
	"counter-help/admin/server/model/customer"
	customerReq "counter-help/admin/server/model/customer/request"
	"counter-help/admin/server/utils"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type CusAuthApi struct{}

// CreateCusAuth 创建cusAuth表
// @Tags CusAuth
// @Summary 创建cusAuth表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body customer.CusAuth true "创建cusAuth表"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /cusAuth/createCusAuth [post]
func (cusAuthApi *CusAuthApi) CreateCusAuth(c *gin.Context) {
	var cusAuth customer.CusAuth
	err := c.ShouldBindJSON(&cusAuth)
	fmt.Println("C:::", cusAuth.SubmitType)
	waitUse, _ := utils.GetClaims(c)
	// UserId
	cusAuth.UserId = int(waitUse.BaseClaims.ID)
	// Status
	if cusAuth.SubmitType == "save" {
		cusAuth.Status = 0
	} else if cusAuth.SubmitType == "submit" {
		cusAuth.Status = 1
	} else {
		cusAuth.Status = 7
	}

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = cusAuthService.CreateCusAuth(&cusAuth)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteCusAuth 删除cusAuth表
// @Tags CusAuth
// @Summary 删除cusAuth表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body customer.CusAuth true "删除cusAuth表"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /cusAuth/deleteCusAuth [delete]
func (cusAuthApi *CusAuthApi) DeleteCusAuth(c *gin.Context) {
	ID := c.Query("ID")
	err := cusAuthService.DeleteCusAuth(ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteCusAuthByIds 批量删除cusAuth表
// @Tags CusAuth
// @Summary 批量删除cusAuth表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /cusAuth/deleteCusAuthByIds [delete]
func (cusAuthApi *CusAuthApi) DeleteCusAuthByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	err := cusAuthService.DeleteCusAuthByIds(IDs)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateCusAuth 更新cusAuth表
// @Tags CusAuth
// @Summary 更新cusAuth表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body customer.CusAuth true "更新cusAuth表"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /cusAuth/updateCusAuth [put]
func (cusAuthApi *CusAuthApi) UpdateCusAuth(c *gin.Context) {
	var cusAuth customer.CusAuth
	err := c.ShouldBindJSON(&cusAuth)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = cusAuthService.UpdateCusAuth(cusAuth)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindCusAuth 用id查询cusAuth表
// @Tags CusAuth
// @Summary 用id查询cusAuth表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query customer.CusAuth true "用id查询cusAuth表"
// @Success 200 {object} response.Response{data=customer.CusAuth,msg=string} "查询成功"
// @Router /cusAuth/findCusAuth [get]
func (cusAuthApi *CusAuthApi) FindCusAuth(c *gin.Context) {
	ID := c.Query("ID")
	recusAuth, err := cusAuthService.GetCusAuth(ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(recusAuth, c)
}

// GetCusAuthList 分页获取cusAuth表列表
// @Tags CusAuth
// @Summary 分页获取cusAuth表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query customerReq.CusAuthSearch true "分页获取cusAuth表列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /cusAuth/getCusAuthList [get]
func (cusAuthApi *CusAuthApi) GetCusAuthList(c *gin.Context) {
	var pageInfo customerReq.CusAuthSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := cusAuthService.GetCusAuthInfoList(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	// TODO:: 和客户前端链接 增加返回字段 用户名和手机号
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetCusAuthPublic 不需要鉴权的cusAuth表接口
// @Tags CusAuth
// @Summary 不需要鉴权的cusAuth表接口
// @accept application/json
// @Produce application/json
// @Param data query customerReq.CusAuthSearch true "分页获取cusAuth表列表"
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /cusAuth/getCusAuthPublic [get]
func (cusAuthApi *CusAuthApi) GetCusAuthPublic(c *gin.Context) {
	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的cusAuth表接口信息",
	}, "获取成功", c)
}
